/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import com.openbravo.data.loader.TableDBSentenceBuilder;
import e.odbo.data.dao.ModifiedLogDAO;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.sqlbuilder.QBFBuilder;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;

import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.format.Formats;
import com.openbravo.bean.Service;
import com.openbravo.data.model.Field;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class ServiceDAO extends ModifiedLogDAO<Service>{

    public ServiceDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(
                "SERVICE",
                new Field[]{
                          new Field("ID",Datas.STRING,Formats.STRING),
                          new Field("NAME",Datas.STRING,Formats.STRING),
                          new Field("SERVICETYPE",Datas.STRING,Formats.STRING),
                          new Field("REF_ID",Datas.STRING,Formats.STRING),
                          new Field("ATTRSETINST_ID",Datas.STRING,Formats.STRING),
                          new Field("RANTCYCLESTYLE",Datas.STRING, Formats.STRING),
                          new Field("CYCLEPRICE",Datas.DOUBLE,Formats.CURRENCY),
                          new Field("CUSTOMERCYCLEPRICE",Datas.DOUBLE,Formats.CURRENCY),
                          new Field("RANTSTATE",Datas.STRING,Formats.STRING),
                          new Field("SERVICECALENDAR_ID",Datas.STRING,Formats.STRING),
                          new Field("REFERENCE",Datas.STRING,Formats.STRING),
                          new Field("ATTRIBUTESET_ID",Datas.STRING,Formats.STRING),
                          new Field("COST",Datas.DOUBLE,Formats.CURRENCY),
                          new Field("TAXCAT",Datas.STRING,Formats.STRING),
                          new Field("MUTEX",Datas.BOOLEAN,Formats.BOOLEAN),
                          new Field("ROUNDRULE",Datas.INT,Formats.INT),
                          new Field("CYCLEMULTIPLE",Datas.INT,Formats.INT),
                          new Field("LASTMODIFIED",Datas.TIMESTAMP, Formats.TIMESTAMP)
                },
                new int[]{0});
    }

    @Override
    public void writeInsertValues(DataWrite dp, Service obj) throws BasicException {
         dp.setString(1,obj.getId());
         dp.setString(2,obj.getM_sName());
         dp.setString(3,obj.getM_sType());
         dp.setString(4,obj.getM_sRefId());
         dp.setString(5,obj.getAttSetInst());
         dp.setString(6,obj.getRantCycleStyle());
         dp.setDouble(7, obj.getCyclePrice());
         dp.setDouble(8,obj.getCustomerCyclePrice());
         dp.setString(9, obj.getRantState());
         dp.setString(10,obj.getServiceCalenderId());
         dp.setString(11,obj.getReference());
         dp.setString(12,obj.getAttributeSetId());
         dp.setDouble(13,obj.getM_dCost());
         dp.setString(14,obj.getTaxcategoryid());
         dp.setBoolean(15,obj.isMutex());
         dp.setInt(16,obj.getRoundRule());
         dp.setTimestamp(17,obj.getLastModified());
    }

    @Override
    public void writeUpdateValues(DataWrite dp, Service obj) throws BasicException {
         dp.setString(1,obj.getId());
         dp.setString(2,obj.getM_sName());
         dp.setString(3,obj.getM_sType());
         dp.setString(4,obj.getM_sRefId());
         dp.setString(5,obj.getAttSetInst());
         dp.setString(6,obj.getRantCycleStyle());
         dp.setDouble(7, obj.getCyclePrice());
         dp.setDouble(8,obj.getCustomerCyclePrice());
         dp.setString(9, obj.getRantState());
         dp.setString(10,obj.getServiceCalenderId());
         dp.setString(11,obj.getReference());
         dp.setString(12,obj.getAttributeSetId());
         dp.setDouble(13,obj.getM_dCost());
         dp.setString(14,obj.getTaxcategoryid());
         dp.setBoolean(15,obj.isMutex());
         dp.setInt(16,obj.getRoundRule());
         dp.setTimestamp(17,obj.getLastModified());

         dp.setString(18,obj.getId());
    }  

    public Service readValues(DataRead dr,Service s) throws BasicException {
            if(s==null)
            	s = new Service();
            s.setId(dr.getString(1));
            s.setM_sName(dr.getString(2));
            s.setM_sType(dr.getString(3));
            s.setM_sRefId(dr.getString(4));
            s.setAttSetInst(dr.getString(5));
            s.setRantCycleStyle(dr.getString(6));
            s.setCyclePrice(dr.getDouble(7));
            s.setCustomerCyclePrice(dr.getDouble(8));
            s.setRantState(dr.getString(9));
            s.setServiceCalenderId(dr.getString(10));
            s.setReference(dr.getString(11));
            s.setAttributeSetId(dr.getString(12));
            s.setM_dCost(dr.getDouble(13));
            s.setTaxcategoryid(dr.getString(14));
            s.setMutex(dr.getBoolean(15));
            s.setRoundRule(dr.getInt(16));
            s.setLastModified(dr.getTimestamp(17));
            return s;
    }

     public Service findService(String serviceType, String srcId) throws BasicException {
        return (Service) new PreparedSentence(s, ((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE SERVICETYPE = ? AND  REF_ID = ?", new SerializerWriteBasic(Datas.STRING, Datas.STRING), this).find(serviceType, srcId);
    }

     public Service findServiceById(String id) throws BasicException {
        return (Service) new PreparedSentence(s, ((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE ID = ?", SerializerWriteString.INSTANCE, this).find(id);
    }


    public List<Service> list(Object filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE ?(QBF_FILTER)", new String[]{"ID", "SERVICETYPE", "REF_ID"}), new SerializerWriteBasic(Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING), this).list(filter);
    }

    @Override
    public Class getSuportClass() {
        return Service.class;
    }

}
